﻿
onefolder    = false
norootclose  = true
norootselect = true
closesubs    = false

function tree (a_items, a_template, f_getchild,treeContainerId ) 
{
	this.a_tpl      = a_template;
	this.a_config   = a_items;
	this.o_root     = this;
	this.a_index    = [];
	this.o_selected = null;
	this.n_depth    = -1;
	this.f_getchild = f_getchild;       //取下级结点的函数
	
	var o_icone = new Image(),
		o_iconl = new Image();
	o_icone.src = a_template['icon_e'];
	o_iconl.src = a_template['icon_l'];
	a_template['im_e'] = o_icone;
	a_template['im_l'] = o_iconl;
	for (var i = 0; i < 64; i++)
		if (a_template['icon_' + i]) {
			var o_icon = new Image();
			a_template['im_' + i] = o_icon;
			o_icon.src = a_template['icon_' + i];
		}
	
	//add by ghqian 2007-10-12
	this.toggleByItem = tree_toggleByItem;
	this.toggleByData = tree_toggleByData;          //只能展开已经生成的节点，不处理待动态生成的节点
	this.selectByData = tree_selectByData;          //展开到指定节点（不处理待动态生成的节点），选择该节点
		
	this.toggle = function (n_id) 
	{		
		var o_item = this.a_index[n_id]; 
		this.toggleByItem( o_item , false )		
	};

	this.select = function (n_id) 
	{ 
		return this.a_index[n_id].select(); 
	};
		
	this.mout   = function (n_id) 
	{ 
		this.a_index[n_id].upstatus(true) 
	};
	
	this.mover  = function (n_id) 
	{ 
		this.a_index[n_id].upstatus() 
	};

	this.a_children = [];
	for (var i = 0; i < a_items.length; i++)
		new tree_item(this, i);

	this.n_id = trees.length;
	trees[this.n_id] = this;
	
	//HTML Container add by ghqian 2007-10-24
	var container = null;
	if( treeContainerId )
	    container = document.getElementById(treeContainerId);
	
	if( container)
	    container.innerHTML = "";
	
	for (var i = 0; i < this.a_children.length; i++) 
	{   
	    var mHtml = this.a_children[i].init();
	    if( container )
	        container.innerHTML += mHtml;
	     else
		    document.write( mHtml );
		this.a_children[i].open();
	}
}

////////////// toggle & select by data Add by ghqian 2007-10-12///////////////////
//展开到指定节点（不处理待动态生成的节点），选择该节点
//如果runEvent为true，同时出发对应事件
function tree_selectByData( m_data, runEvent )
{
    for( var i=0;i<this.a_index.length;i++)
	{
		var o_item = this.a_index[i];
		if( o_item.m_data==m_data )
		{
		    var parentItem = o_item.o_parent;
			if( parentItem != null )
				this.toggleByItem(parentItem ,true);					
			//this.toggleByItem(o_item,true);
			
			o_item.select();
			if( runEvent )
			    eval( o_item.a_config[1] );
			return;				
		}
	}   
}

function tree_toggleByItem( o_item, toggleParent )
{
    if( o_item && o_item.n_depth>-1)		
    {    
        if( toggleParent )
        {
	        var parentItem = o_item.o_parent;
	        if( parentItem && parentItem.n_depth>-1 && !parentItem.b_opened )
		        this.toggleByItem( parentItem, toggleParent );
        }
    	
	    if( o_item && !o_item.b_opened) 
	    {
		    if (onefolder) 
			    expand_or_collapse_level(this,o_item.n_depth)
	    };
	    o_item.open(o_item.b_opened) 
	 }
}
	
//根据m_data 展开  Add by ghqian 2007-10-11
function tree_toggleByData( m_data )
{
	for( var i=0;i<this.a_index.length;i++)
	{
		var o_item = this.a_index[i];
		if( o_item.m_data==m_data )
		{
			this.toggleByItem(o_item,true);
			return;				
		}
	}
}


function tree_item (o_parent, n_order) {

	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 4 : 0)];
	if (!this.a_config) return;

	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	this.b_opened  = !this.n_depth;
	this.b_havechild = this.a_config[2];    //add by ghqian
	this.m_data      = this.a_config[3];    //add by ghqian 

	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 4; i++)
		new tree_item(this, i);

	this.get_icon = item_get_icon;
	this.open     = item_open;
	this.select   = item_select;
	this.init     = item_init;
	this.upstatus = item_upstatus;
	
	this.is_last  = function () 
	{ 
		return this.n_order == this.o_parent.a_children.length - 1 
	};
}

//将item的子结点生成HTML联合起来放到o_idiv里  add by ghqian 2007-10-11
function item_open_joinChildHtml(o_idiv,item)
{
	var a_children = [];		
    for (var i = 0; i < item.a_children.length; i++) 
	    a_children[i]= item.a_children[i].init();
	
    o_idiv.innerHTML = a_children.join('');
}

//处理item_open的善后问题 add by ghqian 2007-10-11
function item_open_tail(o_idiv,item,b_close)
{
	var o_jicon = document.images['j_img' + item.o_root.n_id + '_' + item.n_id],
	    o_iicon = document.images['i_img' + item.o_root.n_id + '_' + item.n_id];
	
    //判断是否取到了下级数据,取不到下级数据时做如下处理
    if( o_idiv.innerHTML=="" )
    {	    
       //移除DIV
       if( o_idiv.parentNode )
         o_idiv.parentNode.removeChild( o_idiv );
       else
         o_idiv.style.display="none";		    
	
        //替换加减号等图片
        item.b_opened = false;	        
        item.b_havechild = false;
        if (o_jicon) o_jicon.src = item.get_icon(true); 
        
        //移出<a>元素的事件
        var po = o_jicon.parentNode;
        if( po )
        {	            
            po.outerHTML = po.innerHTML;
        }
    }
    else
    {    	
        item.b_opened = !b_close;	        
        if (o_jicon) o_jicon.src = item.get_icon(true);
    }    	
    
    if (o_iicon) o_iicon.src = item.get_icon();
    item.upstatus();
}

//添加临时节点，提示Loading... add by ghqian 2007-10-11
function item_open_addTipItem(o_idiv,item)
{
    item.a_config[item.a_config.length] = ["<strong>Loading...</strong>","",false];
    var tipItem = new tree_item(item,0);	        
    o_idiv.innerHTML = tipItem.init();	     
    item.a_config.splice( item.a_config.length-1,1 );
    item.a_children.splice( 0,item.a_children.length );
}

//动态添加下级节点 add by ghqian 2007-10-11
function item_open_dynGetChild(o_idiv,item,b_close)
{
 	//取下级结点	        
    var childItem = item.o_root.f_getchild(item.m_data);
    if( childItem )
    {	            
        for( var m=0;m<childItem.length;m++)
            item.a_config[item.a_config.length] = childItem[m];
    }
    for (var i = 0; i < item.a_config.length - 4; i++) 
        new tree_item(item, i); 
        
    item_open_joinChildHtml(o_idiv,item);

    //善后
    item_open_tail(o_idiv,item,b_close)	
} 

//modify by ghqian 2007-10-11  修正Loading...
function item_open (b_close) 
{
     if (!(this.n_depth==0 && b_close && norootclose)) 
     {  //**** to avoid collapsing of first voice
        //**** Added to close subfolders ****
	    if (!manageall && closesubs) 
	    {
	    	for (var i = 0; i < this.a_children.length; i++)
	        	this.a_children[i].open(true)
	    }
	    //***********************************
    	
    	var meItem = this;		//add by ghqian 2007-10-11
	    var o_idiv = get_element('i_div' + this.o_root.n_id + '_' + this.n_id);
	    if (!o_idiv) return;
    	
	    o_idiv.style.display = (b_close ? 'none' : 'block');
    	
	    if (!o_idiv.innerHTML) 
	    {	    
	        //如果没有下级结点，但属性显示有下级结点，说明需要动态加载下级结点
	        if( !this.a_children.length && this.b_havechild )
	        {
	            //将提示信息结点加上去
	            item_open_addTipItem(o_idiv,meItem);
    	        
	            //取下级结点	        
	            setTimeout( function(){ item_open_dynGetChild(o_idiv,meItem,b_close) } ,0)
	            return;		            
	        }
	        	
		    item_open_joinChildHtml(o_idiv,meItem);
	    }
	    
	    //善后
	    item_open_tail(o_idiv,meItem,b_close)	    
     }	
}

function item_select (b_deselect) 
{
//     if (!(this.n_depth==0 && norootselect)) {  //**** to avoid selection of first voice
	if (!b_deselect) 
	{
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
	}
	
	var o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_iicon) 
		o_iicon.src = this.get_icon();
		
	get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
//     }

     this.upstatus();
     return Boolean(this.a_config[1]);
     	
}

function item_upstatus (b_clear) 
{
	//window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);
}

function item_init () 
{   
	var a_offset = [],
		o_current_item = this.o_parent;
		
	for (var i = this.n_depth; i > 1; i--)
	{
		a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.is_last() ? 'icon_e' : 'icon_l'] + '" border="0" align="absbottom">';
		o_current_item = o_current_item.o_parent;
	}
	
	return '<table cellpadding="0" cellspacing="0" border="0"><tr><td nowrap>' + 
	        (this.n_depth ? 
	                       a_offset.join('') + (
	                                             (this.a_children.length>0||this.b_havechild) ?
	                                                   '<a onfocus=this.blur() href="" onClick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ');trees[' + this.o_root.n_id + '].select(' + this.n_id + ');return false" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><img src="' + this.get_icon(true) + '" border="0" align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		                                               : '<img src="' + this.get_icon(true) + '" border="0" align="absbottom">')
		                    : '') 
	                 + '<a href="#" onfocus=this.blur();  onclick=trees[' + this.o_root.n_id + '].select(' + this.n_id + ');'+this.a_config[1]+' ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ');return trees[' + this.o_root.n_id + '].select(' + this.n_id + ')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" class="t' + this.o_root.n_id + 'i" id="i_txt' + this.o_root.n_id + '_' + this.n_id + '"><img src="' + this.get_icon() + '" border="0" align="absbottom" name="i_img' + this.o_root.n_id + '_' + this.n_id + '" class="t' + this.o_root.n_id + 'im">' + this.a_config[0] + '</a></td></tr></table>' + ((this.a_children.length>0||this.b_havechild) ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
}

function item_get_icon (b_junction) 
{   
	return this.o_root.a_tpl['icon_' + ((this.n_depth ? 0 : 32) + ((this.a_children.length>0||this.b_havechild) ? 16 : 0) + ((this.a_children.length>0||this.b_havechild) && this.b_opened ? 8 : 0) + (!b_junction && this.o_root.o_selected == this ? 4 : 0) + (b_junction ? 2 : 0) + (b_junction && this.is_last() ? 1 : 0))];
}

var trees = [];
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

var manageall=false

function expand_or_collapse_all (o_tree, b_collapse) {     
     manageall=true
     for (var i = 1; i < o_tree.a_index.length; i++) 
     {
        var o_item = o_tree.a_index[i];
        if( o_item.a_children.length>0 )
            o_item.open(b_collapse);
     }
     manageall=false
}

function expand_or_collapse_level (o_tree, level) 
{     
     for (var i = 1; i < o_tree.a_index.length; i++) 
     {
        var o_item = o_tree.a_index[i];
        if (o_item.n_depth==level && o_item.a_children.length>0 ) 
        	o_item.open(true);
     }
}

//For ScriptManage by ghqian 2007-10-25
if( typeof(Sys)!="undefined" && Sys.Application) {     
      Sys.Application.notifyScriptLoaded();
 } 